<?php

/**
 * @date 2015-8-26
 * @version
 */

namespace Common\Model;

class MenuModel extends CommonModel
{
    protected $pk        =  'menu_id';
    protected $tableName =  'menu';
    protected $token     =  'bao_menu';
    protected $orderby   =  array('orderby' => 'asc');
    protected $local     =  array();

    //静态验证
    protected $_validate = array(
        array('menu_name', 'require', '请填写菜单名称！'),
    );

    //动态验证
    protected $rules = array(
        array('menu_name', 'require', '请填写菜单名称！'),
        array('menu_action', 'require', '请填写控制器名称！')
    );
   
    public function checkAuth($auth)
    {
        $data = $this->fetchAll();
        foreach ($data as $row) {
            if ($auth == $row['menu_action']) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获得对应菜单
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function findMenu($id)
    {
        return $this->where(array('menu_id' => $id))->find();
    }

    /**
     * 添加/编辑菜单
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function saveData()
    {
        $data = I('post.', false);
        if ($this->create($data)) {
            if ($data['menu_id']) {
                $result = $this->save($data);
            } else {
                $result = $this->add($data);
            }
            if ($result || $result !== false) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 添加/编辑子菜单
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function saveAction()
    {
        $parent_id = I("parent_id");
        $data = I('post.data', false);
        $new  = I('post.new', false);
        if (!empty($data)) {
            foreach ($data as $k => $val) {
                $this->local = array(
                    'menu_id' => $k,
                    'menu_name' => htmlspecialchars($val['menu_name'], ENT_QUOTES, 'UTF-8'),
                    'menu_action' => htmlspecialchars($val['menu_action'], ENT_QUOTES, 'UTF-8'),
                    'orderby' => $val['orderby'],
                    'is_show' => $val['is_show'],
                );
                if ($this->validate($this->rules)->create($this->local)) {
                    $this->save($this->local);
                } else {
                    return false;
                }
            }
        }
        if (!empty($new)) {
            foreach ($new as $k => $val) {
                $this->local = array(
                    'parent_id'    =>  $parent_id,
                    'menu_name'    =>  htmlspecialchars($val['menu_name'], ENT_QUOTES, 'UTF-8'),
                    'menu_action'  =>  htmlspecialchars($val['menu_action'], ENT_QUOTES, 'UTF-8'),
                    'orderby'      =>  $val['orderby'],
                    'is_show'      =>  $val['is_show'],
                );
                if ($this->validate($this->rules)->create($this->local)) {
                    $this->add($this->local);
                } else {
                    return false;
                }
            }
        }
        $this->cleanCache();
    }
}